using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint.Utilities;

namespace PropertyManagementFeatures
{
	public sealed partial class PropertyManagementWorkflowFeature: SharePointSequentialWorkflowActivity
	{
        public Guid workflowId = default(System.Guid);
        public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();

        public String HistoryDescription = default(string);
        public String HistoryOutcome = default(string);
        public int userID = -1;

        public Guid taskId = default(System.Guid);
        public SPWorkflowTaskProperties taskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties taskBeforeProperties = new SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties taskAfterProperties = new SPWorkflowTaskProperties();
        public Guid TaskStatusFieldId = new Guid("c15b34c3-ce7d-490a-b133-3f4de8801b76");
        public string TaskStatus = default(string);
        public bool TaskNotComplete = true;
        public String taskOutcome = default(System.String);

        public string ExecutingUser = default(string);

        public String propertyEvaluationTaskContentTypeId = "0x0108010084565D92BEFE4a75A28C2F658B7B1234";
        public String propertyOnlineTaskContentTypeId = "0x0108010084565D92BEFE4a75A28C2F658B7B1222";
        public String propertySaleTaskContentTypeId = "0x0108010084565D92BEFE4a75A28C2F658B7B3333";

		public PropertyManagementWorkflowFeature()
		{
			InitializeComponent();
		}

  
        private void onWorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
        {
            this.workflowId = this.workflowProperties.WorkflowId;
            userID = workflowProperties.OriginatorUser.ID;
        }

        private void logWorkflowActivated_MethodInvoking(object sender, EventArgs e)
        {
            SPListItem item = workflowProperties.Item;
            HistoryDescription = "Property Management Workflow started on item " + item.Title;
            HistoryOutcome = "Workflow activation complete";
        }


        private void create_MethodInvoking(object sender, EventArgs e)
        {
            taskId = Guid.NewGuid();

            taskProperties.Title = "Property Evaluate Task ";
            taskProperties.Description = "Please evaluate the property ";
            taskProperties.AssignedTo = this.workflowProperties.OriginatorUser.LoginName;
            taskProperties.PercentComplete = 0;
            taskProperties.StartDate = DateTime.Today;
            taskProperties.DueDate = DateTime.Today.AddDays(14);
        }

        private void logEvaluationTaskCreated_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Property management task assinged to " + taskProperties.AssignedTo;
            TaskStatus = taskAfterProperties.ExtendedProperties[TaskStatusFieldId].ToString();
            HistoryOutcome = "Task status: " + TaskStatus;
        }

        private void onEvaluationTaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            if (taskAfterProperties.ExtendedProperties["TaskStatus"] != null)
            {
                TaskStatus = taskAfterProperties.ExtendedProperties["TaskStatus"].ToString();
                if (TaskStatus.Equals("Completed"))
                {
                    TaskNotComplete = false;
                    
                }
      
                HistoryOutcome = this.DumpHashtable(taskAfterProperties.ExtendedProperties);
            }

            userID = GetUserIdFromName(workflowProperties.Web, ExecutingUser);
        }

        private void completeEvaluationTask_MethodInvoking(object sender, EventArgs e)
        {
            TaskNotComplete = true;
        }

        #region "Helper Methods"

        private void EnsureUserIsRecognized(string user)
        {
            if (GetUserIdFromName(workflowProperties.Web, user) == -1)
            {
                workflowProperties.Web.SiteUsers.Add(user, "", "", "");
            }
        }

        private int GetUserIdFromName(SPWeb site, string UserName)
        {
            SPPrincipalInfo PrincipalInfo;
            PrincipalInfo = SPUtility.ResolvePrincipal(site,
                                                       UserName,
                                                       SPPrincipalType.All,
                                                       SPPrincipalSource.All,
                                                       null,
                                                       false);
            if (PrincipalInfo == null)
            {
                return -1;
            }
            else
            {
                return PrincipalInfo.PrincipalId;
            }
        }

        private string DumpHashtable(Hashtable table)
        {
            string temp = string.Empty;
            foreach (DictionaryEntry entry in table)
            {
                if (!entry.Key.ToString().Contains("-"))
                {
                    temp += entry.Key.ToString() + "=" + entry.Value + " | ";
                }
            }
            return temp;
        }

        #endregion  

       

        private void logPublishingTaskCreated_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Property management task assinged to " + taskProperties.AssignedTo;
            TaskStatus = taskAfterProperties.ExtendedProperties[TaskStatusFieldId].ToString();
            HistoryOutcome = "Task status: " + TaskStatus;
        }

        private void onPublishingTaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            if (taskAfterProperties.ExtendedProperties["TaskStatus"] != null)
            {
                TaskStatus = taskAfterProperties.ExtendedProperties["TaskStatus"].ToString();
                if (TaskStatus.Equals("Completed"))
                {
                    TaskNotComplete = false;

                }

                HistoryOutcome = this.DumpHashtable(taskAfterProperties.ExtendedProperties);
            }

            userID = GetUserIdFromName(workflowProperties.Web, ExecutingUser);

        }

        private void completePublishingTask_MethodInvoking(object sender, EventArgs e)
        {
            TaskNotComplete = true;
        }

        private void createPublishingTask_MethodInvoking(object sender, EventArgs e)
        {
            taskId = Guid.NewGuid();

            taskProperties.Title = "Property Publishing Task ";
            taskProperties.Description = "Please publish the property into market ";
            taskProperties.AssignedTo = this.workflowProperties.OriginatorUser.LoginName;
            taskProperties.PercentComplete = 0;
            taskProperties.StartDate = DateTime.Today;
            taskProperties.DueDate = DateTime.Today.AddDays(14);

        }

        private void logPublishingTaskCompleted_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Publishing Task Completed.";
            HistoryOutcome = "Task status: Task Completed";
        }

        private void logEvaluationTaskCompleted_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Evaluation Task Completed.";
            HistoryOutcome = "Task status: Task Completed";
        }


        private void onSaleTaskCreated_Invoked(object sender, ExternalDataEventArgs e)
        {
          
        }

        private void logSaleTaskCreated_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Property Sale task assinged to " + taskProperties.AssignedTo;
            TaskStatus = taskAfterProperties.ExtendedProperties[TaskStatusFieldId].ToString();
            HistoryOutcome = "Task status: " + TaskStatus;
        }

        private void onSaleTaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            if (taskAfterProperties.ExtendedProperties["TaskStatus"] != null)
            {
                TaskStatus = taskAfterProperties.ExtendedProperties["TaskStatus"].ToString();
                if (TaskStatus.Equals("Completed"))
                {
                    TaskNotComplete = false;

                }

                HistoryOutcome = this.DumpHashtable(taskAfterProperties.ExtendedProperties);
            }

            userID = GetUserIdFromName(workflowProperties.Web, ExecutingUser);
        }

        private void logSaleTaskCompleted_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Property Sale task completed ";
            HistoryOutcome = "Task status: Task Completed";
        }

        private void logWorkflowCompleted_MethodInvoking(object sender, EventArgs e)
        {
            HistoryDescription = "Workflow completed ";
            HistoryOutcome = "Workflow completed  ";
        }


        private void onPropertyEvaluationTaskCreated_Invoked(object sender, ExternalDataEventArgs e)
        {

        }

        private void createSaleTask_MethodInvoking(object sender, EventArgs e)
        {
            taskId = Guid.NewGuid();

            taskProperties.Title = "Property Sale Task ";
            taskProperties.Description = "Please sell the property ";
            taskProperties.AssignedTo = this.workflowProperties.OriginatorUser.LoginName;
            taskProperties.PercentComplete = 0;
            taskProperties.StartDate = DateTime.Today;
            taskProperties.DueDate = DateTime.Today.AddDays(14);
        }

        
	}

  

}
